package com.tanhua.app.Service;

import com.tanhua.app.exception.BusinessException;
import com.tanhua.config.template.AipTemplate;
import com.tanhua.config.template.OssTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;



@Service
public class UserInfoService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private AipTemplate aipTemplate;

    //    首次登录---完善资料
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    //    首次登录---补充头像
    public void updateHead(MultipartFile headPhoto, Long id) throws Exception {
        String url = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        boolean detect = aipTemplate.detect(url);
        if (!detect) {
            throw new BusinessException(ErrorResult.faceError());
        }else {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setAvatar(url);
            userInfoApi.update(userInfo);
        }
    }

    //    用户资料 - 读取
    public UserInfoVo findById(Long id) {
        UserInfo userInfo = userInfoApi.findById(id);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        if(userInfo.getAge()!=null){
            userInfoVo.setAge(userInfo.getAge().toString());
        }
        return userInfoVo;
    }

    //  用户资料 = 保存
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }
}
